package com.sbl.bajw.pc.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.sbl.bajw.common.MyBatisDao;
import com.sbl.bajw.common.web.JsonResultUtil;
import com.sbl.bajw.common.web.RequestUnit;
import com.sbl.bajw.mapper.DeptModelDao;
import com.sbl.bajw.pc.model.DeptModel;

@Service
public class DeptService {
	
	@Resource
	private DeptModelDao dao;
	
	@Resource
	private MyBatisDao gridDao;
	
	public List<DeptModel> getDept(String pid){
		List<DeptModel> list = dao.getOrgByCons(pid);
		return list;
	}
	
	public List<DeptModel> getOrgPid(String pid){
		
		HashMap<String, Object> parm = new HashMap<String, Object>();
		parm.put("pid", pid);
		
		List<DeptModel> list = dao.getOrgPid(parm);
		return list;
	}
	
	public DeptModel getOrgById(String id){
		DeptModel model = dao.getOrgById(id);
		return model;
	}
	
	public List<DeptModel> getOrgByIds(){
		String idsStr = RequestUnit.getString("deptids");
		if(idsStr == null){
			return null;
		}
		String[] ids = idsStr.split(",");
		HashMap<String, Object> parm = new HashMap<String, Object>();
		parm.put("ids", ids);
		List<DeptModel> depts = dao.getDeptByIds(parm);
		return depts;
	}
	
	public String getId(){
		String id = dao.getId();
		return id;
	}
	
	public List<DeptModel> getOrgByLevel(String level,String fullid){
		HashMap<String, Object> parm = new HashMap<String, Object>();
		parm.put("level", level);
		if(fullid != null && !fullid.equals("")){
			parm.put("fullid", fullid+"%");
		}
		
		List<DeptModel> list = dao.getOrgByLevel(parm);
		return list;
	}
	
	public List<Map<String, Object>> getInitOrg(HashMap<String, Object> parm){
		List<Map<String, Object>> list = dao.getInitOrg(parm);
		return list;
	}
	
	public String getOrgManagerList(){
		String id = RequestUnit.getString("id");
		String depttype = RequestUnit.getString("depttype");
		String pid = RequestUnit.getString("pid");
		
		HashMap<String, Object> parm = new HashMap<String, Object>();
		parm.put("id", id);
		parm.put("depttype", depttype);
		parm.put("pid", pid);
		
		String json = gridDao.selectPage(DeptModelDao.class, parm, "getOrgManagerList");
		String result = JsonResultUtil.Success(json);
		return result;
	}
	
	public List<DeptModel> getOrgManagerListAll(){
		List<DeptModel> models = dao.getOrgManagerList(null);
		return models;
	}
	
	public void update(DeptModel model){
		if(model.getId() == null || model.getId().equals("")){
			return;
		}
		dao.update(model);
	}
	
	public void insert(DeptModel model){
		if(model.getId() == null || model.getId().equals("")){
			return;
		}
		dao.insert(model);
	}
}
